﻿using System.Security.Cryptography;
using System.Text;

namespace DiffuseEnjoyIdle.Core.Utility
{
    public class SecurityUtil
    {
        #region MD5加密


        // <summary>
        /// MD5加密密码
        /// </summary>
        public static string MD5Encrypt(string psw)
        {
            // 第四步：使用MD5加密
            var md5 = MD5.Create();
            var bytes = md5.ComputeHash(Encoding.UTF8.GetBytes(psw));

            var result = new StringBuilder();
            for (var i = 0; i < bytes.Length; i++)
            {
                result.Append(bytes[i].ToString("X2"));
            }
            return result.ToString();
        }
        /// <summary>
        /// MD5加密
        /// </summary>
        public static byte[] MD5Encrypt(string inputString, Encoding encoding = null)
        {
            if (encoding == null)
            {
                encoding = Encoding.UTF8;
            }
            var bytes = encoding.GetBytes(inputString);//求Byte[]数组
            MD5 md5 = MD5.Create();
            return md5.ComputeHash(bytes);

        }

        /// <summary>
        /// MD5加密转16进制字符串
        /// </summary>
        public static string MD5Encrypt2Hex(string inputString, Encoding encoding = null)
        {
            byte[] md5 = MD5Encrypt(inputString, encoding);
            return BytesUtil.ToHex(md5);
        }

        #endregion

        #region SHA1加密

        /// <summary>
        /// SHA1加密
        /// </summary>
        public static byte[] SHA1Encrypt(string inputString, Encoding encoding = null)
        {
            if (encoding == null)
            {
                encoding = Encoding.UTF8;
            }
            var bytes = encoding.GetBytes(inputString);
            var shal = SHA1.Create();

            return shal.ComputeHash(bytes);

        }

        /// <summary>
        /// SHA1加密转16进制字符串
        /// </summary>
        public static string SHA1Encrypt2Hex(string inputString, Encoding encoding = null)
        {
            byte[] sha1 = SHA1Encrypt(inputString, encoding);
            return BytesUtil.ToHex(sha1);
        }


        #endregion
    }
}
